/**
 * 独立的进度管理器
 * 完全不依赖React组件状态，避免触发重新渲染
 */

import { useDataLineageProgressManager } from '../components/DataLineageProgressDisplay';

class IsolatedProgressManager {
  private progressHooks: ReturnType<typeof useDataLineageProgressManager> | null = null;
  
  // 设置进度管理hooks
  setProgressHooks(hooks: ReturnType<typeof useDataLineageProgressManager>) {
    this.progressHooks = hooks;
  }

  // 启动进度
  startProgress(dataSource: string): string | null {
    if (!this.progressHooks) return null;
    return this.progressHooks.startProgress(dataSource);
  }

  // 更新进度（不触发组件重新渲染）
  updateProgress(
    notificationId: string,
    progress: number,
    stage: string,
    currentItem?: string,
    statistics?: any
  ) {
    if (!this.progressHooks || !notificationId) return;
    
    // 直接调用进度更新，不通过组件状态
    this.progressHooks.updateAnalysisProgress(
      notificationId,
      progress,
      stage,
      statistics
    );
  }

  // 完成进度
  completeProgress(
    notificationId: string,
    success: boolean,
    summary?: string,
    statistics?: any
  ) {
    if (!this.progressHooks || !notificationId) return;
    
    this.progressHooks.completeAnalysisProgress(
      notificationId,
      success,
      summary,
      statistics
    );
  }
}

// 创建全局实例
export const isolatedProgressManager = new IsolatedProgressManager();

// React Hook包装器
export const useIsolatedProgressManager = () => {
  const progressHooks = useDataLineageProgressManager();
  
  // 设置hooks到全局管理器
  isolatedProgressManager.setProgressHooks(progressHooks);
  
  return {
    startProgress: (dataSource: string) => isolatedProgressManager.startProgress(dataSource),
    updateProgress: (
      notificationId: string,
      progress: number,
      stage: string,
      currentItem?: string,
      statistics?: any
    ) => isolatedProgressManager.updateProgress(notificationId, progress, stage, currentItem, statistics),
    completeProgress: (
      notificationId: string,
      success: boolean,
      summary?: string,
      statistics?: any
    ) => isolatedProgressManager.completeProgress(notificationId, success, summary, statistics),
  };
};
